const errcode = require('../utils/errcode')
const logger = require('../utils/logger')

module.exports = async function (ctx, next) {
    ctx.success = function (data) {
        ctx.body = { ...errcode.OK, data }
    }

    ctx.error = function (err, data) {
        ctx.body = { ...err, data }
    }

    ctx.login = ctx.session.user

    try {
        await next()
    } catch (err) {
        logger.error(err)
        ctx.error(errcode.UNKNOWN)
    }
}
